package csel.model;

public class RealTile implements Tile, java.io.Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Map map;
	Entity entity;
	Item item;
	Location location;
	Terrain terrain;
	Decal decal;
	AreaEffect areaEffect;

	public RealTile()
	{

	}

	public Tile getNeighbor(Direction direction)
	{
		Tile neighbor;
		int myX = location.getX(), myY = location.getY();
		switch (direction)
		{
		case NORTHWEST:
			neighbor = map.getTile(myX - 1, myY - 1);
			break;
		case NORTHEAST:
			neighbor = map.getTile(myX + 1, myY - 1);
			break;
		case NORTH:
			neighbor = map.getTile(myX, myY - 1);
			break;
		case SOUTHWEST:
			neighbor = map.getTile(myX - 1, myY + 1);
			break;
		case SOUTHEAST:
			neighbor = map.getTile(myX + 1, myY + 1);
			break;
		case SOUTH:
			neighbor = map.getTile(myX, myY + 1);
			break;
		case EAST:
			neighbor = map.getTile(myX + 1, myY);
			break;
		case WEST:
			neighbor = map.getTile(myX - 1, myY);
			break;
		default:
			return null;
		}
		return (neighbor);
	}

	public boolean enter(Entity e)
	{
		if((entity == null)&&(terrain.canPass(e)))
		{
			if(item != null)
			{
				if(item.isImpassable())
				{
					return(false);
				}
				/*if(Model.infoMode)*/System.out.println("[MODEL] the item on this tile is of type "+ item.getClass().getSimpleName());
				item.touch(e);
			}
			if(areaEffect != null)
			{
				areaEffect.enter(e);
			}
			entity = e;
			return(true);
		}
		else
		{
			return(false);
		}
	}

	public boolean leave(Entity e)
	{
//		if(entity.equals(e))
//		{
			entity = null;
			if(areaEffect != null)
			{
				areaEffect.leave(e);
			}
			return(true);
//		}
//		else
//		{
//			return(false);
//		}
	}

	public Entity getEntity()
	{
		return (entity);
	}

	public Item getItem()
	{
		return (item);
	}

	public Location getLocation()
	{
		return location;
	}

	public Terrain getTerrain()
	{
		return terrain;
	}

	public void setLocation(Location location)
	{
		this.location = location;
	}

	public void setEntity(Entity e)
	{
		this.entity = e;
	}

	public void setItem(Item i)
	{
		this.item = i;
	}

	public void setTerrain(Terrain t)
	{
		this.terrain = t;
	}

	public Map getMap()
	{
		return map;
	}

	public void setMap(Map map)
	{
		this.map = map;
	}

	public Decal getDecal() {
		return decal;
	}

	public void setDecal(Decal decal) {
		this.decal = decal;
	}

	public AreaEffect getAreaEffect()
	{
		return areaEffect;
	}

	public void setAreaEffect(AreaEffect areaEffect)
	{
		this.areaEffect = areaEffect;
	}
	
	
}
